package com.ld.security.server.admin.modules.ge.vo;

import com.ld.security.common.core.base.SuperVo;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.ScriptUtil;
import com.ld.security.common.mybatis.anno.QueryKey;
import com.ld.security.common.mybatis.anno.RemoveKey;
import com.ld.security.common.mybatis.anno.SaveKey;
import com.ld.security.common.mybatis.anno.SqlKey;
import com.ld.security.common.mybatis.base.vo.ProxyVo;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.server.admin.modules.ge.constant.GeEarlyFieldconstant;
import com.ld.security.server.admin.modules.ge.entity.GeEarlyFieldEntity;
import com.ld.security.server.admin.modules.ge.entity.GeEarlyModelEntity;
import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ApiModel("预警详情")
@Data
public class EarlyDetailsVo implements SuperVo,ProxyVo {

    private List<String> modelIds;

    private List<EarlyDetails> earlyModels;


    @RemoveKey
    @QueryKey
    @SqlKey(name = "modelId",value = SqlCondition.in ,valueField = "modelIds")
    private List<GeEarlyModelEntity> earlyModelList;


    @RemoveKey
    @QueryKey(groupKey = "earlyModelId")
    @SqlKey(name = "earlyModelId",value = SqlCondition.in,valueField = "earlyModelList.id")
    private Map<String,List<GeEarlyFieldEntity>> earlyFieldMap;


    @Data
    public static class EarlyDetails{

        @SaveKey
        private GeEarlyModelEntity earlyModel;

        @SaveKey(name = "earlyModelId",valueField = "earlyModel.id")
        private List<GeEarlyFieldEntity> earlyFieldList;


        /**
         * 校验对象是否满足条件
         * @param obj
         * @return
         */
        public boolean check(Object obj){

            Boolean val = null;
            StringBuffer sb =   new StringBuffer();

          for (GeEarlyFieldEntity fieldEntity : earlyFieldList) {
              String tarVal =fieldEntity.getEarlyValue();
              String value = ""+ ReflectionUtil.invokeGetter(obj,fieldEntity.getFieldName());
              //字段校验
              boolean fieldResult = false;
              switch (fieldEntity.getSqlKey()){
                  case ge:{
                      double x =  Double.parseDouble(value);
                      double y =  Double.parseDouble(tarVal);
                      if(x >= y){
                          fieldResult = true;
                      }
                      break;
                  }case gt:{
                      double x =  Double.parseDouble(value);
                      double y =  Double.parseDouble(tarVal);
                      if(x > y){
                          fieldResult = true;
                      }
                      break;
                  }case le:{
                      double x =  Double.parseDouble(value);
                      double y =  Double.parseDouble(tarVal);
                      if(x <= y){
                          fieldResult = true;
                      }
                      break;
                  }case lt:{
                      double x =  Double.parseDouble(value);
                      double y =  Double.parseDouble(tarVal);
                      if(x < y){
                          fieldResult = true;
                      }
                      break;
                  }
                  case eq:{
                        if(tarVal.equals(value)){
                            fieldResult = true;
                        }
                      break;
                  }case like_left:{
//                      if(value.startsWith(tarVal)){
//                          fieldResult = true;
//                      }
                  }case like_right:{
//                      if(value.endsWith(tarVal)){
//                          fieldResult = true;
//                      }
                  }case like:{
                      if(value.indexOf(tarVal) != -1){
                          fieldResult = true;
                      }
                      break;
                  }case between:{
                      String[] strs =  tarVal.split(",");
                      Double x =  Double.parseDouble(strs[0]);
                      Double y =  Double.parseDouble(strs[1]);
                      Double v =  Double.parseDouble(value);
                      if(v >= x && v <= y){
                          fieldResult = true;
                      }
                      break;
                  }case in:{
                      String[] strs =  tarVal.split(",");
                      for (String str : strs) {
                        if(str.equals(value)){
                            fieldResult = true;
                            break;
                        }
                      }
                      break;
                  }case ne:{
                      if(!tarVal.equals(value)){
                          fieldResult = true;
                      }
                      break;
                  }
              }

              if(val == null){
                  val = fieldResult;
              }else if(fieldEntity.getOperator() == GeEarlyFieldconstant.operator.and){
                  val = val && fieldResult;
              }else{
                  val = val || fieldResult;
              }

          }
          if(val == null){
              val = false;
          }


            return  val;
        }


    }


    @Override
    public void queryAfter() {

        this.earlyModels =  earlyModelList.stream().map(v -> {
            List<GeEarlyFieldEntity> fs =  earlyFieldMap.get(v.getId());
            EarlyDetails model = new EarlyDetails();
            model.setEarlyModel(v);
            model.setEarlyFieldList(fs);
            return model;
        }).collect(Collectors.toList());


    }
}
